package com.xxzb.pack0822.多线程.线程调度;

public class 线程让步Yield {
    public static void main(String[] args) {
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 1000; i++) {
                    System.out.println(Thread.currentThread().getName() + ":" + i);
                }
            }
        }, "t1");
        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 1000; i++) {
                    System.out.println(Thread.currentThread().getName() + ":" + i);
                    if (i>900){
                        Thread.yield();//线程让步,意思就是t2给t1让步，让t1早早执行完，所以最后执行完的是t2
                    }
                }
            }
        }, "t2");
        t1.start();
        t2.start();
    }
}
